[PR]

水無瀬の部屋 > Programming > sample > goddess > usespi.cpp
最終更新日: 2007/03/29

   1: //*********************************************************
   2: // プロジェクト: ああっ壁神さまっ
   3: //   ファイル名: usespi.cpp
   4: //*********************************************************
   5: #include "goddess.h"
   6: #include <susie/susie.h>
   7: #include <filefmt/bmpfile.h>
   8: 
   9: 
  10: //---------------------------------------------------------
  11: // 構造体 の 宣言
  12: //---------------------------------------------------------
  13: // SUSIE_t
  14: typedef struct SUSIE_tag
  15: {
  16: 	HINSTANCE        hInstance;
  17: 	IsSupported_t    IsSupported;
  18: 	GetPluginInfo_t  GetPluginInfo;
  19: 	GetPicture_t     GetPicture;
  20: 	char             filename[ MAX_PATH_BUF ];
  21: }SUSIE_t;
  22: 
  23: // 読み込んだ画像
  24: typedef struct LOADPICTUREPROCPARAM_tag
  25: {
  26: 	BYTE             *img;
  27: 	BITMAPINFOHEADER *bih;
  28: 	RGBQUAD          *rgb;
  29: 	char              filename[ MAX_PATH_BUF ];
  30: 	BYTE              head[2048];
  31: 	int               length;
  32: } LOADPICTUREPROCPARAM_t;
  33: 
  34: 
  35: //---------------------------------------------------------
  36: // ファイルスコープ関数 の 宣言
  37: //---------------------------------------------------------
  38: static BYTE *GetPictureImage( const SUSIE_t *spi, const char *filename, BITMAPINFOHEADER *bih, RGBQUAD *rgb );
  39: static bool  UnloadSusiePlugin( SUSIE_t *spi );
  40: static bool  LoadSusiePlugin( SUSIE_t *spi, const char *filename, int want );
  41: static void *LocalToPtr( HLOCAL hMem, int want );
  42: static bool  GetBitmapInfoFromLocalHandle( HLOCAL hMem, BITMAPINFOHEADER *pbih, RGBQUAD *rgb );
  43: 
  44: 
  45: //*********************************************************
  46: // _enum_plugin
  47: //*********************************************************
  48: static 
  49: bool 
  50: CALLBACK 
  51: _enum_plugin
  52: 	(
  53: 		const char            *spiname, 
  54: 		const WIN32_FIND_DATA *,
  55: 		      void            *param
  56: 	)
  57: {
  58: 	// パラメタの仮定
  59: 	ASSERT( IsPathExist( spiname ) );
  60: 	ASSERT( IsValidPtr( (LOADPICTUREPROCPARAM_t *)param, sizeof(LOADPICTUREPROCPARAM_t) ) );
  61: 	ASSERT( IsPathFile( ((LOADPICTUREPROCPARAM_t *)param)->filename ) );
  62: 	ASSERT( IsValidPtr( ((LOADPICTUREPROCPARAM_t *)param)->bih, sizeof(BITMAPINFOHEADER) ) );
  63: 	ASSERT( IsValidPtr( ((LOADPICTUREPROCPARAM_t *)param)->rgb, 256 * sizeof(RGBQUAD) ) );
  64: 	ASSERT( null == ((LOADPICTUREPROCPARAM_t *)param)->img );
  65: 
  66: 	SUSIE_t spi;
  67: 	if ( ! LoadSusiePlugin( &spi, spiname, SPI_TYPE_IMPORT ) )
  68: 	{
  69: 		// 次のプラグインへ
  70: 		return true;
  71: 	}
  72: 
  73: 	LOADPICTUREPROCPARAM_t *lppp = (LOADPICTUREPROCPARAM_t *)param;
  74: 	if ( Susie_IsSupported( spi.IsSupported, lppp->filename, lppp->head, lppp->length ) )
  75: 	{
  76: 		lppp->img = GetPictureImage( &spi, lppp->filename, lppp->bih, lppp->rgb );
  77: 		if ( lppp->img )
  78: 		{
  79: 			ASSERT( sizeof(BITMAPINFOHEADER) == lppp->bih->biSize );
  80: 			UnloadSusiePlugin( &spi );
  81: 			return false; // ここで探索を打ち切る
  82: 		}
  83: 	}
  84: 
  85: 	UnloadSusiePlugin( &spi );
  86: 	ASSERT( null == lppp->img );
  87: 
  88: 	// 次のプラグインへ
  89: 	return true;
  90: }//_enum_plugin
  91: 
  92: //*********************************************************
  93: // LoadPicture
  94: //*********************************************************
  95: BYTE *
  96: LoadPicture_susie
  97: 	(
  98: 		const char             *filename,
  99: 		      BITMAPINFOHEADER *bih,
 100: 		      RGBQUAD          *rgb,
 101: 		const char             *plugin,
 102: 		const char             *spi
 103: 	)
 104: {
 105: 	// パラメタの仮定
 106: 	ASSERT( IsPathExist( plugin ) );
 107: 	ASSERT( IsPathFile( filename ) );
 108: 	ASSERT( IsValidStringPtr( spi ) );
 109: 	ASSERT( IsValidPtr( bih, sizeof( *bih ) ) );
 110: 	ASSERT( IsValidPtr( rgb, 256 * sizeof( *rgb ) ) );
 111: 
 112: 
 113: 	// ファイルのヘッダ情報を取得
 114: 	LOADPICTUREPROCPARAM_t lppp;
 115: 	memzero( &lppp, sizeof( lppp ) );
 116: 	strcopy( lppp.filename, numof(lppp.filename), filename );
 117: 	lppp.bih = bih;
 118: 	lppp.rgb = rgb;
 119: 	lppp.img = null;
 120: 	FILE *fp = fopen( lppp.filename, "rb" );
 121: 	if ( !fp )
 122: 	{
 123: 		return null;
 124: 	}
 125: 	lppp.length = (int)fread( lppp.head, 1, sizeof(lppp.head), fp );
 126: 	VERIFY( 0 == fclose( fp ) );
 127: 	ASSERT( (0 <= lppp.length) && (lppp.length <= sizeof(lppp.head)) );
 128: 
 129: 	VERIFY( SetCurrentDirectory( plugin ) ); 
 130: 	EnumFile( plugin, spi, _enum_plugin, &lppp );
 131: 	return lppp.img;
 132: }//LoadPicture
 133: 
 134: 
 135: //******************************************************************************************************************
 136: //
 137: //******************************************************************************************************************
 138: //*********************************************************
 139: // LoadSusiePlugin
 140: //*********************************************************
 141: static
 142: bool
 143: LoadSusiePlugin
 144: 	(
 145: 		      SUSIE_t *spi,
 146: 		const char    *filename,
 147: 		      int      want
 148: 	)
 149: {
 150: 	// パラメタの仮定
 151: 	ASSERT( IsPathFile( filename ) );
 152: 	ASSERT( IsValidPtr( spi, sizeof( *spi ) ) );
 153: 
 154: 	//
 155: 	spi->hInstance     = null;
 156: 	spi->GetPluginInfo = null;
 157: 	spi->IsSupported   = null;
 158: 	spi->GetPicture    = null;
 159: 	strcopy( spi->filename, numof(spi->filename), filename );
 160: 
 161: 	// プラグインのロード
 162: 	VERIFY( spi->hInstance = LoadLibrary( filename ) );
 163: 	if ( !spi->hInstance ) 
 164: 	{
 165: 		return false;
 166: 	}
 167: 
 168: 	// 関数のロード
 169: 	spi->GetPluginInfo = (GetPluginInfo_t)GetProcAddress( spi->hInstance, "GetPluginInfo" );
 170: 	if ( !IsValidCodePtr( spi->GetPluginInfo ) )
 171: 	{
 172: 		UnloadSusiePlugin( spi ); // VERIFY(  );
 173: 		return false;
 174: 	}
 175: 
 176: 	int type = 0;
 177: 	if ( !Susie_GetPluginInfo( spi->GetPluginInfo, &type ) )
 178: 	{
 179: 		UnloadSusiePlugin( spi ); // VERIFY(  );
 180: 		return false;
 181: 	}
 182: 
 183: 	if ( want != (type & want) )
 184: 	{
 185: 		UnloadSusiePlugin( spi ); // VERIFY(  );
 186: 		return false;
 187: 	}
 188: 
 189: 	spi->IsSupported = (IsSupported_t)GetProcAddress( spi->hInstance, "IsSupported" );
 190: 	spi->GetPicture  = (GetPicture_t)GetProcAddress(  spi->hInstance, "GetPicture"  );
 191: 	if ( !IsValidCodePtr( spi->IsSupported )
 192: 	  || !IsValidCodePtr( spi->GetPicture ) )
 193: 	{
 194: 		UnloadSusiePlugin( spi );
 195: 		return false;
 196: 	}
 197: 
 198: 	return true;
 199: }//LoadSusiePlugin
 200: 
 201: //*********************************************************
 202: // UnloadSusiePlugin
 203: //*********************************************************
 204: static 
 205: bool
 206: UnloadSusiePlugin
 207: 	(
 208: 		SUSIE_t *spi
 209: 	)
 210: {
 211: 	// パラメタの仮定
 212: 	ASSERT( IsValidPtr( spi, sizeof( *spi ) ) );
 213: 	ASSERT( !spi->GetPluginInfo || IsValidCodePtr( spi->GetPluginInfo ) );
 214: 	ASSERT( !spi->IsSupported   || IsValidCodePtr( spi->IsSupported ) );
 215: 	ASSERT( !spi->GetPicture    || IsValidCodePtr( spi->GetPicture ) );
 216: 	ASSERT( spi->hInstance );
 217: 
 218: 	const bool bSuccess = (0 != FreeLibrary( spi->hInstance ));
 219: 
 220: 	spi->hInstance     = null;
 221: 	spi->GetPluginInfo = null;
 222: 	spi->IsSupported   = null;
 223: 	spi->GetPicture    = null;
 224: 	return bSuccess;
 225: }//UnloadSusiePlugin
 226: 
 227: //*********************************************************
 228: // GetPictureImage
 229: //*********************************************************
 230: static
 231: BYTE *
 232: GetPictureImage
 233: 	(
 234: 		const SUSIE_t          *spi,
 235: 		const char             *filename,
 236: 		      BITMAPINFOHEADER *bih,
 237: 		      RGBQUAD          *rgb
 238: 	)
 239: {
 240: 	// パラメタの仮定
 241: 	ASSERT( IsPathFile( filename ) );
 242: 	ASSERT( IsValidReadPtr( spi, sizeof( *spi ) ) );
 243: 	ASSERT( IsValidPtr( bih, sizeof( *bih ) ) );
 244: 	ASSERT( IsValidPtr( rgb, 256 * sizeof( *rgb ) ) );
 245: 	ASSERT( IsValidCodePtr( spi->GetPicture ) );
 246: 
 247: 	HLOCAL hImage, hBmpInfo;
 248: 	if ( !Susie_GetPicture( spi->GetPicture, filename, &hBmpInfo, &hImage ) )
 249: 	{
 250: 		return null;
 251: 	}
 252: 	
 253: 	ASSERT( IsValidLocalMemoryEx( hBmpInfo, sizeof( *bih ), INT_MAX ) );
 254: 	if ( ! GetBitmapInfoFromLocalHandle( hBmpInfo, bih, rgb ) )
 255: 	{
 256: 		return null;
 257: 	}
 258: 
 259: 	const int size = CalcBitmapImageSize( bih );
 260: 	ASSERT( IsValidLocalMemoryEx( hImage, size, INT_MAX ) );
 261: 
 262: 	BYTE *pBits = (BYTE *)LocalToPtr( hImage, size );
 263: 
 264: 	VERIFY( !LocalFree( hBmpInfo ) );
 265: 	VERIFY( !LocalFree( hImage ) );
 266: 	return pBits;
 267: }//GetPictureImage
 268: 
 269: //*********************************************************
 270: // GetBitmapInfoFromLocalHandle
 271: //*********************************************************
 272: static
 273: bool
 274: GetBitmapInfoFromLocalHandle
 275: 	(
 276: 		HLOCAL            hBmpInfo, 
 277: 		BITMAPINFOHEADER *bih,
 278: 		RGBQUAD          *rgb
 279: 	)
 280: {
 281: 	// パラメタの仮定
 282: 	ASSERT( IsValidLocalMemoryEx( hBmpInfo, sizeof( *bih ), INT_MAX ) );
 283: 	ASSERT( IsValidPtr( bih, sizeof( *bih ) ) );
 284: 	ASSERT( IsValidPtr( rgb, 256 * sizeof( *rgb ) ) );
 285: 
 286: 	BITMAPINFO *bi = (BITMAPINFO *)LocalToPtr( hBmpInfo, sizeof( *bih ) );
 287: 	if ( !bi )
 288: 	{
 289: 		return false;
 290: 	}
 291: 	if ( sizeof( *bih ) != bi->bmiHeader.biSize )
 292: 	{
 293: 		free( bi );
 294: 		return false;
 295: 	}
 296: 
 297: 	*bih = bi->bmiHeader;
 298: 	ASSERT( sizeof( *bih ) == bih->biSize );
 299: 
 300: 	const int size = (int)LocalSize( hBmpInfo );
 301: 	memcpy( rgb, &bi->bmiColors, min(256 * sizeof( *rgb ), size - bih->biSize) );
 302: 	free( bi );
 303: 
 304: 	return true;
 305: }//GetBitmapInfoFromLocalHandle
 306: 
 307: //*********************************************************
 308: // LocalToPtr
 309: //*********************************************************
 310: static
 311: void *
 312: LocalToPtr
 313: 	(
 314: 		HLOCAL hMem,
 315: 		int    want
 316: 	)
 317: {
 318: 	// パラメタの仮定
 319: 	ASSERT( LMEM_INVALID_HANDLE != LocalFlags( hMem ) );
 320: 	VERIFY( 0 < want );
 321: 	
 322: 	if ( LMEM_INVALID_HANDLE == LocalFlags( hMem ) )
 323: 	{
 324: 		return null;
 325: 	}
 326: 
 327: 	const int size = (int)LocalSize( hMem );
 328: 	if ( size < want )
 329: 	{
 330: 		return null;
 331: 	}
 332: 
 333: 	void *src = (void *)LocalLock( hMem );
 334: 	if ( !src )
 335: 	{
 336: 		return null;
 337: 	}
 338: 
 339: 	if ( ! IsValidPtr( src, size ) )
 340: 	{
 341: 		return null;
 342: 	}
 343: 
 344: 	void *dst = (void *)memdup( src, size );
 345: 
 346: 	LocalUnlock( hMem );
 347: 
 348: 	return dst;
 349: }//LocalToPtr
 350: 
 351: 
 352: //** end **
 353: 

参照:


Google
ご意見・ご感想をお聞かせ下さい。匿名で送信できます。

 * 返信が必要な場合には postmaster@katsura-kotonoha.sakura.ne.jp へ直接メールしてください。

水無瀬の部屋 > sample > goddess > usespi.cpp

このページは cpp2web が出力しました。
水無瀬 優 postmaster@katsura-kotonoha.sakura.ne.jp
http://katsura-kotonoha.sakura.ne.jp/prog/code/goddess/usespi_cpp.shtml
『新妻LOVELY×CATION』を応援しています!